import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import  java.util.*;

public class FileMethods {

    public String getFileName(){
        int randomNumber=0;
        String fileName="";

        Random randomGenerator = new Random();
        randomNumber = randomGenerator.nextInt(30000);
        fileName =Integer.toString(randomNumber);
        return fileName;
    }

    public String getFileType(String url) throws IOException {

        URLConnection connection;
        URL urlConnect;
        String type="";
        String checkType="";

        urlConnect=new URL(url);
        type = url.substring(url.lastIndexOf("."));
        connection = urlConnect.openConnection();
        checkType= type.replace(".","");
        if((connection.getContentType()).contains(checkType)){return type;}
        else{return ".html";}
    }
    public void writeFile(String url, byte[] byteOut) throws IOException {
        File file;
        FileOutputStream FOS;

        file = new File(getFileName()+ getFileType(url));
        file.createNewFile();
        FOS= new FileOutputStream(file.getAbsolutePath());
        FOS.write(byteOut);
        FOS.close();
    }
}
